package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.ShopTransCount;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.TimeUtil;
import com.tool.utils.utils.StringUtils;

import org.json.JSONObject;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopTransCountActivity extends BaseActivity implements View.OnClickListener {


    private static final String TAG = "ShopTransCountActivity";
    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.tv_order_money)
    TextView tvOrderMoney;
    @BindView(R.id.tv_coupon_deduction_count)
    TextView tvCouponDeductionCount;
    @BindView(R.id.tv_coupon_deduction_amt)
    TextView tvCouponDeductionAmt;
    @BindView(R.id.tv_coupon_send_count)
    TextView tvCouponSendCount;
    @BindView(R.id.tv_coupon_send_num)
    TextView tvCouponSendNum;
    @BindView(R.id.tv_point_deduction_count)
    TextView tvPointDeductionCount;
    @BindView(R.id.tv_point_deduction_amt)
    TextView tvPointDeductionAmt;
    @BindView(R.id.tv_point_deduction_num)
    TextView tvPointDeductionNum;
    @BindView(R.id.tv_point_send_count)
    TextView tvPointSendCount;
    @BindView(R.id.tv_point_send_num)
    TextView tvPointSendNum;
    @BindView(R.id.tv_sale_member_count)
    TextView tvSaleMemberCount;
    @BindView(R.id.tv_sale_member_amt)
    TextView tvSaleMemberAmt;
    @BindView(R.id.tv_new_member_count)
    TextView tvNewMemberCount;
    @BindView(R.id.tv_old_member_count)
    TextView tvOldMemberCount;
    @BindView(R.id.tv_un_member_count)
    TextView tvUnMemberCount;
    @BindView(R.id.tv_un_member_amt)
    TextView tvUnMemberAmt;
    @BindView(R.id.tv_member_add_count)
    TextView tvMemberAddCount;
    @BindView(R.id.tv_verification_count)
    TextView tvVerificationCount;
    @BindView(R.id.tv_verification_amt)
    TextView tvVerificationAmt;
    @BindView(R.id.tv_settle_amt)
    TextView tvSettleAmt;
    @BindView(R.id.tv_stores_trans_count)
    TextView tvStoresTransCount;
    @BindView(R.id.tv_stores_trans_total)
    TextView tvStoresTransTotal;
    @BindView(R.id.tv_stores_settle_trans_total)
    TextView tvStoresSettleTransTotal;


    private int TYPE_TIME_OPTION = 1;
    private int TYPE_PAY_OPTION = 2;

    private final int REQUEST_CODE_QUERYDATE_FROM = 1;
    private final int REQUEST_CODE_QUERYDATE_TO = 2;

//    private PullToRefreshScrollView pullToRefreshScrollView;

    private Date panel_dateFrom = new Date();
    private Date panel_dateTo = new Date();
    private Date query_dateFrom = panel_dateFrom;
    private Date query_dateTo = panel_dateTo;


    private int page = 1;
    private int rows = Config.ROWS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_shop_trans_count);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "店铺交易统计报表");
        showView(R.id.next_sure, false);

        String from = TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日");
        setTvText(R.id.tv_date_choose, from);
        findViewById(R.id.tv_date_choose).setOnClickListener(this);
        findViewById(R.id.titleback_btn_back).setOnClickListener(this);
        findViewById(R.id.ly_previous).setOnClickListener(this);
        findViewById(R.id.ly_next).setOnClickListener(this);

        showNextDayBtn();

        getdata();
    }

    @OnClick(R.id.titleback_btn_back)
    public void onViewClicked() {
    }


    private void refreshData() {
        page = 1;
        getdata();
    }

    private void getdata() {
        showProgress("加载中...");
        RequestParams params = new RequestParams();

        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");
        params.put("statisDate", TimeUtil.getStringFromDate(query_dateFrom, "yyyy-MM-dd"));


        HttpUtil.get(Config.SBS_SHOP_TRANS_COUNT, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {

                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }

                    Gson gson = new Gson();
                    // json数据转换成List
                    ShopTransCount txRecord = gson.fromJson(result.optString("data"), new TypeToken<ShopTransCount>() {
                    }.getType());

                    if (txRecord == null) {
                        return;
                    }

                    tvNum.setText(txRecord.getTotalNumber() + "");
                    tvTotalMoney.setText(StringUtils.formatIntMoney(txRecord.getTotalRealAmount()));
                    tvOrderMoney.setText(StringUtils.formatIntMoney(txRecord.getTotalAmount()));
                    tvCouponDeductionCount.setText(txRecord.getTotalCouponNumber() + "");
                    tvCouponDeductionAmt.setText(StringUtils.formatIntMoney(txRecord.getTotalCouponAmount()));
                    tvCouponSendCount.setText(txRecord.getTotalSendCouponNumber() + "");
                    tvCouponSendNum.setText(txRecord.getTotalCouponCount() + "");
                    tvPointDeductionCount.setText(txRecord.getTotalBonusCount() + "");
                    tvPointDeductionAmt.setText(StringUtils.formatIntMoney(txRecord.getTotalBonusAmount()));
                    tvPointDeductionNum.setText(txRecord.getTotalBonusNumber() + "");
                    tvPointSendCount.setText(txRecord.getTotalSendBonusNumber() + "");
                    tvPointSendNum.setText(txRecord.getTotalSendBonusCount() + "");
                    tvSaleMemberCount.setText(txRecord.getTotalMemberNumber() + "");
                    tvSaleMemberAmt.setText(StringUtils.formatIntMoney(txRecord.getTotalMemberAmount()));
                    tvNewMemberCount.setText(txRecord.getNewMemberCount() + "");
                    tvOldMemberCount.setText(txRecord.getOldMemberCount() + "");
                    tvUnMemberCount.setText(txRecord.getNoMemberCustomerCount() + "");
                    tvUnMemberAmt.setText(StringUtils.formatIntMoney(txRecord.getNoMemberCustomerAmount()));
                    tvMemberAddCount.setText(txRecord.getAddMemberCount() + "");
                    tvVerificationCount.setText(txRecord.getTotalVeriftyCouponCount() + "");
                    tvVerificationAmt.setText(StringUtils.formatIntMoney(txRecord.getTotalVeriftyAmount()));
                    tvSettleAmt.setText(StringUtils.formatIntMoney(txRecord.getTotalSettingPriceAmount()));
                    tvStoresTransCount.setText(txRecord.getEntntyGoodNumber()+"");
                    tvStoresTransTotal.setText(StringUtils.formatIntMoney(txRecord.getEntntyGoodRealAmount()));
                    tvStoresSettleTransTotal.setText(StringUtils.formatIntMoney(txRecord.getEntntyGoodSettingAmount()));



                } catch (Exception e) {
                    e.getMessage();
                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.ly_previous:
                query_dateFrom = TimeUtil.getDateFromDate(query_dateFrom, -1);
                query_dateTo = query_dateFrom;
                panel_dateFrom = query_dateFrom;
                panel_dateTo = query_dateTo;
                setTvText(R.id.tv_date_choose, TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日"));
                showNextDayBtn();
                refreshData();
                break;
            case R.id.ly_next:
                query_dateFrom = TimeUtil.getDateFromDate(query_dateFrom, 1);
                query_dateTo = query_dateFrom;
                panel_dateFrom = query_dateFrom;
                panel_dateTo = query_dateTo;
                setTvText(R.id.tv_date_choose, TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日"));
                showNextDayBtn();
                refreshData();
                break;
            case R.id.tv_date_choose:
                DatePickDialog dialog = new DatePickDialog(this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        query_dateFrom = date;
                        setTvText(R.id.tv_date_choose, TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日"));
//                        tv_date_from.setText(TimeUtil.getStringFromDate(date, "yyyy-MM-dd"));
                        showNextDayBtn();
                        refreshData();
                    }
                });
                dialog.show();
                break;
            default:
                break;
        }
    }


    private void showNextDayBtn() {
        String a = TimeUtil.getStringFromDate(query_dateFrom, "yyyyMMdd");
        String b = TimeUtil.getStringFromDate(query_dateTo, "yyyyMMdd");
        String c = TimeUtil.getCurrentDateString("yyyyMMdd");
        showView(R.id.ly_next, !(a.equals(b) && b.equals(c)));
    }


}
